/*
 * ApproveListAction
 * 
 * Version 1.0
 *
 * 22-08-08
 * 
 * Modification Logs:
 *   DATE        AUTHOR      DESCRIPTION
 *  --------------------------------------------------------
 *  22-8-08		TuHA		declare class prototype
 *  23-8-08		TuHA		code execute method
 */

package vn.fpt.fsoft.fcos.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import vn.fpt.fsoft.fcos.bl.ApproveListBL;
import vn.fpt.fsoft.fcos.entity.UserEntity;
import vn.fpt.fsoft.fcos.form.ApproveListForm;
import vn.fpt.fsoft.fcos.sessionentity.ApproveListSearchCond;
import vn.fpt.fsoft.fcos.util.Constant;
import vn.fpt.fsoft.fcos.sessionentity.SumOrderSEntity;

/**
 * ApproveListAction
 * @author tu
 *
 */
public class ApproveListAction extends Action {

	/**
	 * @param mapping is the ActionMapping object 
	 * @param form is the LoginForm object
	 * @param request is the HttpRequest object of this process
	 * @param reponse is the HttpReponse object of this process
	 * @return the ActionForward object of this action 
	 */
	public ActionForward execute (ActionMapping mapping,
								ActionForm form,
								HttpServletRequest request,
								HttpServletResponse response) throws Exception {
		
		// Perform casting form into an instance of ApproveListForm (approveListForm)
		ApproveListForm approveListForm = (ApproveListForm) form;
		
		// Check UserEntity is existed in the session with key "SK_USER_ENTITY" or not?
		HttpSession session = request.getSession();
		UserEntity userEntity = (UserEntity) session.getAttribute(Constant.SK_USER_ENTITY);
		
		if (userEntity == null) {
			// if it didn't exist add into the session with a pair data (SK_ERRORCODE, M101):
			session.putValue(Constant.SK_ERRORCODE, Constant.MESSAGE_101);
			
			return mapping.findForward("fail");
		}

		// If processId of approveListForm is empty:
		String processId = approveListForm.getProcessID();
		
		if (processId.compareTo("") == 0) {
			// return ActionForward of "init"
			return mapping.findForward("init");
		}
 
		if(processId.compareTo("APPROVELIST_000") == 0) {
		
		} 
		
		if (processId.compareTo("APPROVELIST_001") == 0) {
			// 3.2
			// Perform step 3.1. with currentPage is increased to 1.

		}
		
		if (processId.compareTo("APPROVELIST_002") == 0) {
			// 3.3
			// Perform step 3.1. with currentPage is decreased down 1.

		}
		
		if (processId.compareTo("APPROVELIST_003") == 0) {
			
		}

		return mapping.findForward("success");
	}
	
	private ActionForward searchProcessing(ActionMapping mapping,
											ApproveListForm approveListForm,
											HttpSession session,
											HttpServletResponse response) throws Exception {
		
		int pageNum = 1;
		int pageSize = 10;
		// 3.1
		// Checking the searching condition (Order date)
		if (approveListForm.getOrderDate() == null) {
			
			// If Order date is null, processing error with message id "M003"
			session.putValue(Constant.SK_ERRORCODE, Constant.MESSAGE_003);
			
			return mapping.findForward("fail");
		}

		long orderDate = 0;
		
		try {
			orderDate = Date.parse(approveListForm.getOrderDate());
		} catch (IllegalArgumentException e) {
			
			// If Order date is incorrect processing error with message id "M004"
			session.putValue(Constant.SK_ERRORCODE, Constant.MESSAGE_004);
			
			return mapping.findForward("fail");
		}

		// Building instance of ApproveListSearchConditionEntity with name approveListSearchCondEnt
		ApproveListSearchCond approveListSearchCondEnt = new ApproveListSearchCond();
		
		// Calling search method of ApproveListBL with parameter approveListSearchCond
		ApproveListBL approveListBL = new ApproveListBL();
		List<SumOrderSEntity> list = approveListBL.search(approveListSearchCondEnt, pageNum, pageSize);
		
		// If no record is returned, processing message M005
		if (list.isEmpty()) {
			session.putValue(Constant.SK_ERRORCODE, Constant.MESSAGE_005);
			
			return mapping.findForward("fail");
		}
		
		// If there is above 1 searched record, go to task 4.

		// Building an instance of SearchPaging

		// Extract searched records of current page. Then put them into the form

		// Return ActionForward of forward name "success"
		return mapping.findForward("success");
	}

}
